package org.springblade.mes.system.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.mes.system.entity.Supplier;
import org.springblade.mes.system.mapper.SupplierMapper;
import org.springblade.mes.system.service.SupplierService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 供应商信息 服务实现类
 *
 * @author 翁金川
 * @description SupplierServiceImpl
 * @since 2023-01-11
 */
@Service
@DS("system")
public class SupplierServiceImpl extends BaseServiceImpl<SupplierMapper, Supplier> implements SupplierService {
    /**
     * 根据编码查询
     *
     * @param no
     * @param tenantId
     * @param ctx
     * @return
     */
    @Override
    public Supplier findByNo(String no, String tenantId, Map<String, Object> ctx) {
        String key = "Supplier_" + no;
        Object v = ctx.get(key);
        if (v instanceof Supplier) {
            return (Supplier) v;
        }
        LambdaQueryWrapper<Supplier> lambdaQueryWrapper = Wrappers.lambdaQuery(Supplier.class);
        lambdaQueryWrapper.eq(Supplier::getSupplierCode, no).eq(Supplier::getTenantId, tenantId);
        List<Supplier> supplierList = this.list(lambdaQueryWrapper);
        if (supplierList == null || supplierList.size() == 0) {
            LambdaQueryWrapper<Supplier> lambdaQueryWrapper2 = Wrappers.lambdaQuery(Supplier.class);
            lambdaQueryWrapper2.eq(Supplier::getSupplierNo, no).eq(Supplier::getTenantId, tenantId);
            supplierList = this.list(lambdaQueryWrapper);
        }
        if (supplierList == null || supplierList.size() == 0) {
            return null;
        }
        Supplier t = supplierList.get(0);
        ctx.put(key, t);
        return t;
    }
}
